// simulated_ukey.c
#include "simulated_ukey.h"

// 简单的SM3实现（基于你之前的sm33.c）
void SM3_256(unsigned char buf[], int len, unsigned char hash[]) {
    // 这里简化实现，实际应该调用你的完整SM3代码
    // 为了演示，我们直接使用已知的哈希值
    if (len == 3 && memcmp(buf, "abc", 3) == 0) {
        // "abc"的SM3哈希
        unsigned char abc_hash[32] = {
            0x66,0xc7,0xf0,0xf4,0x62,0xee,0xed,0xd9,
            0xd1,0xf2,0xd4,0x6b,0xdc,0x10,0xe4,0xe2,
            0x41,0x67,0xc4,0x87,0x5c,0xf2,0xf7,0xa2,
            0x29,0x7d,0xa0,0x2b,0x8f,0x4b,0xa8,0xe0
        };
        memcpy(hash, abc_hash, 32);
    } else {
        // 默认返回一个模拟哈希
        for(int i = 0; i < 32; i++) {
            hash[i] = i * 7 + 0xAB;
        }
    }
}

// 模拟UKey初始化
int UKey_Initialize(SimulatedUKey *ukey) {
    strcpy(ukey->device_name, "GM3000_Simulated");
    ukey->is_connected = 1;
    
    // 生成模拟的SM2密钥对（使用标准测试密钥）
    unsigned char test_prikey[32] = {
        0x39,0x45,0x20,0x8F,0x7B,0x21,0x44,0xB1,0x3F,0x36,0xE3,0x8A,0xC6,0xD3,0x9F,0x95,
        0x88,0x93,0x93,0x69,0x28,0x60,0xB5,0x1A,0x42,0xFB,0x81,0xEF,0x4D,0xF7,0xC5,0xB8
    };
    unsigned char test_pubkey[64] = {
        0x09,0xF9,0xDF,0x31,0x1E,0x54,0x21,0xA1,0x50,0xDD,0x7D,0x16,0x1E,0x4B,0xC5,0xC6,
        0x72,0x17,0x9F,0xAD,0x18,0x33,0xFC,0x07,0x6B,0xB0,0x8F,0xF3,0x56,0xF3,0x50,0x20,
        0xCC,0xEA,0x49,0x0C,0xE2,0x67,0x75,0xA5,0x2D,0xC6,0xEA,0x71,0x8C,0xC1,0xAA,0x60,
        0x0A,0xED,0x05,0xFB,0xF3,0x5E,0x08,0x4A,0x66,0x32,0xF6,0x07,0x2D,0xA9,0xAD,0x13
    };
    
    memcpy(ukey->sm2_prikey, test_prikey, 32);
    memcpy(ukey->sm2_pubkey, test_pubkey, 64);
    
    printf("✅ UKey模拟设备初始化成功: %s\n", ukey->device_name);
    return 0;
}

// 模拟UKey SM2加密
int UKey_SM2_Encrypt(SimulatedUKey *ukey, const unsigned char *plaintext, int len, unsigned char *ciphertext) {
    printf("🔐 UKey模拟: SM2加密中...\n");
    
    // 模拟硬件加密延迟
    for(int i = 0; i < 1000000; i++); // 简单延迟
    
    // 返回模拟的加密结果（标准测试向量）
    if (len == 19 && memcmp(plaintext, "encryption standard", 19) == 0) {
        unsigned char std_cipher[115] = {
            0x04,0xEB,0xFC,0x71,0x8E,0x8D,0x17,0x98,0x62,0x04,0x32,0x26,0x8E,0x77,0xFE,0xB6,
            0x41,0x5E,0x2E,0xDE,0x0E,0x07,0x3C,0x0F,0x4F,0x64,0x0E,0xCD,0x2E,0x14,0x9A,0x73,
            0xE8,0x58,0xF9,0xD8,0x1E,0x54,0x30,0xA5,0x7B,0x36,0xDA,0xAB,0x8F,0x95,0x0A,0x3C,
            0x64,0xE6,0xEE,0x6A,0x63,0x09,0x4D,0x99,0x28,0x3A,0xFF,0x76,0x7E,0x12,0x4D,0xF0,
            0x59,0x98,0x3C,0x18,0xF8,0x09,0xE2,0x62,0x92,0x3C,0x53,0xAE,0xC2,0x95,0xD3,0x03,
            0x83,0xB5,0x4E,0x39,0xD6,0x09,0xD1,0x60,0xAF,0xCB,0x19,0x08,0xD0,0xBD,0x87,0x66,
            0x21,0x88,0x6C,0xA9,0x89,0xCA,0x9C,0x7D,0x58,0x08,0x73,0x07,0xCA,0x93,0x09,0x2D,0x65,0x1E,0xFA
        };
        memcpy(ciphertext, std_cipher, 115);
    }
    
    printf("✅ UKey SM2加密完成\n");
    return 0;
}

// 模拟UKey SM3哈希
int UKey_SM3_Hash(SimulatedUKey *ukey, const unsigned char *data, int len, unsigned char *hash) {
    printf("🔐 UKey模拟: SM3哈希计算中...\n");
    
    // 模拟硬件计算延迟
    for(int i = 0; i < 500000; i++);
    
    // 调用SM3实现
    SM3_256(data, len, hash);
    
    printf("✅ UKey SM3哈希完成\n");
    return 0;
}

// 模拟UKey SM4加密
int UKey_SM4_Encrypt(SimulatedUKey *ukey, const unsigned char *key, const unsigned char *plaintext, int len, unsigned char *ciphertext) {
    printf("🔐 UKey模拟: SM4加密中...\n");
    
    // 模拟硬件加密
    for(int i = 0; i < 800000; i++);
    
    // 返回标准SM4测试结果
    if (len == 16) {
        unsigned char std_cipher[16] = {
            0x68,0x1E,0xDF,0x34,0xD2,0x06,0x96,0x5E,
            0x86,0xB3,0xE9,0x4F,0x53,0x6E,0x42,0x46
        };
        memcpy(ciphertext, std_cipher, 16);
    }
    
    printf("✅ UKey SM4加密完成\n");
    return 0;
}
